//
//  PopoverPresentationController.m
//  OC-LBWeibo
//
//  Created by 李博 on 15/7/10.
//  Copyright © 2015年 libo. All rights reserved.
//

#import "PopoverPresentationController.h"

@interface PopoverPresentationController()
//蒙版
@property(nonatomic,weak)UIView *maskView;

@end

@implementation PopoverPresentationController

- (instancetype)initWithPresentedViewController:(nonnull UIViewController *)presentedViewController presentingViewController:(nonnull UIViewController *)presentingViewController
{
    NSLog(@"%@",presentedViewController);
    NSLog(@"%@",presentingViewController);
    
    self = [super initWithPresentedViewController:presentedViewController presentingViewController:presentingViewController];
    
    return self;
    
}

//- (void)prepareMaskView
//{
//    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(close)];
//    [self.maskView addGestureRecognizer:tap];
//
//}

///  容器视图将要重新布局子视图
- (void)containerViewWillLayoutSubviews
{
    [super containerViewWillLayoutSubviews];
    
    self.maskView.frame = self.containerView.bounds;
//    [self.containerView insertSubview:self.maskView atIndex:0];
    
    self.presentedView.frame = self.presentFrame;
}



- (UIView *)maskView
{
    if (_maskView == nil) {
        UIView *mv = [[UIView alloc]init];
        [mv setBackgroundColor:[[UIColor alloc]initWithWhite:0.0 alpha:0.2]];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(close)];
        [mv addGestureRecognizer:tap];
        [self.containerView insertSubview:mv atIndex:0];
        _maskView = mv;
    }
    return _maskView;
}

- (void)close
{
    NSLog(@"%@",self.presentedViewController);
    [self.presentedViewController dismissViewControllerAnimated:YES completion:nil];
}


@end
